<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title id="subject_manager">电影管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
    <style type="text/css">
        tbody .layui-table-cell{
            height:80px;
            line-height: 80px;
        }
        .layui-form.layuimini-form {
            margin-right: 3%;
        }
        tr:first-child{

        }
        table{
            width: 100%;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索条件区域-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">导演</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sDirector" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">演员</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sActor" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline ">
                            <label class="layui-form-label">电影类型</label>
                            <div class="layui-input-inline">
                                <select name="typeId" >
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button  class="layui-btn layui-btn-warm"  lay-filter="data-reset-btn"><i class="layui-icon">&#xe666;</i> 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--头部工具栏区域-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>
        <!--表格区域-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter" ></table>
        <!--行工具栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <!--添加修改窗口弹出层-->
        <div style="display: none;padding:5px" id="addOrUpdateWindow">
            <table>
                <tr>
                    <td>
                        <form class="layui-form layuimini-form " id="dataFrm" lay-filter="dataFrm">
                <!--主键隐藏域，保存主键-->
                <input type="hidden" name="sId" />
                <div class="layui-form-item">
                    <label class="layui-form-label required">电影名</label>
                    <div class="layui-input-block">
                        <input type="text" name="sName" lay-verify="required" lay-reqtext="电影名不能为空" placeholder="请输入电影名" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">导演</label>
                    <div class="layui-input-block">
                        <input type="text" name="sDirector"  placeholder="请输入导演" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">演员</label>
                    <div class="layui-input-block">
                        <input type="text" name="sActor" placeholder="请输入演员" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">海报</label>
                    <div class="layui-inline">
                        <input type="text" name="picSrc" id="picSrc" class="layui-input" />
                    </div>
                    <button type="button" class="layui-btn" id="spic">
                        <i class="layui-icon">&#xe67c;</i>上传海报
                    </button>
                    <button type="button" class="layui-btn layui-btn-xs layui-btn-danger" id="certainUpload">
                        <i class="layui-icon">&#xe605;</i>确认上传
                    </button>

                </div>
                <div class="layui-form-item" id="rate_mod">
                    <label class="layui-form-label">评分</label>
                    <div class="layui-input-block">
                        <div id="rateMod"></div>
                        <input type="hidden" name="sRate" id="sRate" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">日期</label>
                    <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                        <input type="text"  class="layui-input" id="sDate" name="sDate">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类别</label>
                    <div class="layui-input-block">
                        <select name="typeId">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label  ">点击量</label>
                    <div class="layui-input-block">
                        <input type="text" name="sHit"  value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">简介</label>
                    <div class="layui-input-block">
                        <textarea name="sContent" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </form>
                    </td>
                    <td>
                        <img  alt="缩略图" style="display: none" width="150" height="200" id="preview"/>
                    </td>
                </tr>
            </table>


        </div>
    </div>
</div>
<script src="../../static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','layer','upload','laydate','rate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload = layui.upload,
            laydate = layui.laydate,
            rate = layui.rate,
            layer = layui.layer;

        //执行文件上传实例
        var uploadInst = upload.render({
            elem: '#spic' //绑定元素
            ,url: '/subject/upload/' //上传接口
            ,acceptMime: 'image/*'
            ,auto: false //选择文件后不自动上传
            ,bindAction: '#certainUpload' //指向一个按钮触发上传
            ,choose: function(obj){
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                obj.preview(function(index, file, result) {
                        console.log(index); //得到文件索引
                        console.log(file); //得到文件对象
                        console.log(result); //得到文件base64编码，比如图片
                        $("#preview").attr("src",result);
                        $("#preview").css("display","block");
                })
                }
            ,done: function(res,index,upload){
                console.log(res)
                //将路径写入文本框
                $("#picSrc").val(res.data.src);
            }
            ,error: function(){
                //请求异常回调
            }
        });
        //日期控件
        laydate.render({
            elem: '#sDate' //指定元素
            ,type:'date'
            ,trigger:'click'
        });

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/subject/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'sId', width: 100, title: 'ID', sort: true,align: "center"},
                {field: 'sName', width: 156, title: '电影名称',align: "center"},
                {field: 'sDirector', width: 100, title: '导演', sort: true,align: "center"},
                {field: 'sActor', width: 135, title: '演员',align: "center"},
                {field: 'picSrc', minWidth:60,align: "center",title: '海报路径',templet:function (d) {
                            return "<img src='"+d.picSrc+"' height='80'/>"
                    }},
                {field: 'sRate', width: 75, title: '评分', sort: true, align: "center",templet:function (d) {
                            return d.sRate ? d.sRate : "暂无"
                    }},
                {field: 'sDate', minWidth:160, title: '上映日期', sort: true},
                {field: 'type', width: 120, title: '类型',sort: true, align: "center",templet:function (d) {
                        //d是当前行的数据，然后数据中有一个type对象属性，属性下有个typeName
                        //判断为空不能用d.type.typeName == null，会报错
                        if ($.isEmptyObject(d.type)){
                            return "";
                        }else{
                           return d.type.typeName;
                        }
                    }},
                {field: 'sContent', width: 135, title: '简介', sort: true},
                 {field: 'sHit', width: 90, title: '点击量', sort: true, align: "center"},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function(res,curr,count){ //解决当一页的最后一个数据被删除后，不会跳转到前一页的问题
                //判断当前页码是否大于1且当前页数据量为0
                if(curr > 1 && res.data.length == 0){
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr:pageValue}
                    })
                }
            }
        });

        // 监听模糊查询操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            tableIns.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });

            return false;
        });
        //发送请求查询部门列表（将数据追加到下拉列表中）
        $.get("/type/findType",function (result) {
            //循环追加
            var html = "";
            for (let i = 0;i<result.length;i++){
                html += "<option value='"+result[i].typeId+"'>"+result[i].typeName+"</option>";
                // console.log("id="+result[i].typeId+" name="+result[i].typeName);
            }
            //将网页代码追加到下拉列表中
            $("[name='typeId']").append(html);
            //重新渲染下拉列表
            form.render("select");
        },"json")
        //监听重置操作
        form.on('click(data-reset-btn)',function () {
            $("#search-form").reset();
        });

        /**
         * toolbar监听头部工具栏事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openAddWindow();
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                groupDel(data);
            }

        });
        function groupDel(data){
                var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].sId);
            }

            //提示用户是否确认删除
            layer.confirm('确定要删除这些电影吗？',{icon:3,title:"提示"}, function (index) {
                // obj.del();
                //发送删除请求
                $.post("/subject/groupDel", {"sIdsJSON":JSON.stringify(ids)},function (result) {
                    if (result.success){
                        //成功
                        layer.alert(result.message,{icon:1});
                        //刷新当前数据表格
                        tableIns.reload();
                    }else{
                        //提示
                        layer.alert(result.message,{icon:2})
                    }
                },"json");
                layer.close(index);
            });
        }
        //保存提交地址和窗口的索引
        var url,mainIndex;

        function changeRateMod(flag,layero) {
            //获取到rate_mod
            let mod = layero.context.getElementById("rate_mod");
                console.log("mod="+mod);
                if(mod != null){
                    console.log("rate_mod已找到。。。")
                    console.log("flag="+flag);
                    if (flag == "add"){
                        mod.setAttribute("style","display:none");
                    }else {
                        mod.setAttribute("style","display:block");
                    }
            }
        }

        /**
         * 打开添加窗口
         */
        function openAddWindow(){
            mainIndex = layer.open({
                title: '添加电影',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['65%', '80%'],   //宽，高
                content: $("#addOrUpdateWindow"),   //引用的内容窗口
                success: function (layero) {
                    //提交地址
                    url = "/subject/add";
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    //每次打开，清除预览图
                    layero.context.getElementById("preview").setAttribute("src","#");
                    layero.context.getElementById("preview").setAttribute("style","display:none");
                    //清除评分信息
                    $("#sRate").val("");
                    //隐藏评分区域
                    changeRateMod("add",layero);


                }
            })
        }
        /**
         * 打开修改窗口
         */
        function openUpdateWindow(data){
            mainIndex = layer.open({
                title: '修改电影',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['65%', '80%'],   //宽，高
                content: $("#addOrUpdateWindow"),   //引用的内容窗口
                success: function (layero) {
                    //提交地址
                    url = "/subject/update";
                    //渲染评分组件
                    rate.render({
                        elem: '#rateMod', //绑定元素
                        half: true,
                        text: true,
                        length: 10,
                        value: data.sRate,
                        readonly: false,
                        choose: function (value){   //点击触发回调
                            $("#sRate").val(value);
                        }
                    });
                    //显示评分区域
                    changeRateMod("update",layero);
                    if (data.picSrc!=null){
                        //设置图片预览回显
                        layero.context.getElementById("preview").setAttribute("src",data.picSrc);
                        layero.context.getElementById("preview").setAttribute("style","display:block");
                    }
                    //表单数据回显
                    form.val("dataFrm",data);

                }
            })
        }
        //监听表单提交事件
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            //发送请求
            $.post(url,data.field,function (result) {
                if (result.success){
                    //成功
                    layer.alert(result.message,{icon:1});
                    //刷新当前数据表格
                    tableIns.reload();
                    //关闭当前窗口
                    layer.close(mainIndex);
                }else {
                    //失败
                    layer.alert(result.message,{icon:2});
                }
            },"json");
            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
        //监听行工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openUpdateWindow(data);
                return false;
            } else if (obj.event === 'delete') {
                deleteById(data);
            }
        });
        //删除电影方法
        function deleteById(data) {
            //提示用户是否确认删除
            layer.confirm('确定要删除这个电影吗？',{icon:3,title:"提示"}, function (index) {
                // obj.del();
                //发送删除请求
                $.post("/subject/del", {"sId":data.sId},function (result) {
                    if (result.success){
                        //成功
                        layer.alert(result.message,{icon:1});
                        //刷新当前数据表格
                        tableIns.reload();
                    }else{
                        //提示
                        layer.alert(result.message,{icon:2})
                    }
                },"json");
                layer.close(index);
            });
        }
    });
</script>

</body>
</html>